package com.ruoyi.pay.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.pay.domain.Qrcode;
import com.ruoyi.pay.service.IQrcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pay.mapper.OrderPayInfoMapper;
import com.ruoyi.pay.domain.OrderPayInfo;
import com.ruoyi.pay.service.IOrderPayInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-31
 */
@Service
public class OrderPayInfoServiceImpl implements IOrderPayInfoService
{
    @Autowired
    private OrderPayInfoMapper orderPayInfoMapper;

    @Autowired
    private IQrcodeService qrcodeService;

    /**
     * 查询订单管理
     *
     * @param orderNum 订单管理主键
     * @return 订单管理
     */
    @Override
    public OrderPayInfo selectOrderPayInfoByOrderNum(String orderNum)
    {
        return orderPayInfoMapper.selectOrderPayInfoByOrderNum(orderNum);
    }

    /**
     * 查询订单管理列表
     *
     * @param orderPayInfo 订单管理
     * @return 订单管理
     */
    @Override
    public List<OrderPayInfo> selectOrderPayInfoList(OrderPayInfo orderPayInfo)
    {
        return orderPayInfoMapper.selectOrderPayInfoList(orderPayInfo);
    }

    /**
     * 新增订单管理
     *
     * @param orderPayInfo 订单管理
     * @return 结果
     */
    @Override
    public int insertOrderPayInfo(OrderPayInfo orderPayInfo) throws Exception
    {
        orderPayInfo.setCreateTime(DateUtils.getNowDate());
        orderPayInfo.setOrderStatus("1");
        orderPayInfo.setOrderType("1");
        Qrcode qrcode= new Qrcode();
        qrcode.setQrPrice(orderPayInfo.getOrderPrice());
        qrcode.setQrType(orderPayInfo.getPayWay());
        List<Qrcode> list = qrcodeService.selectQrcodeList(qrcode);
        if(CollectionUtils.isEmpty(list))
        {
            throw new Exception("未匹配到对应的付款码");
        }
        orderPayInfo.setQrId(list.get(0).getQrId());
        return orderPayInfoMapper.insertOrderPayInfo(orderPayInfo);
    }

    /**
     * 修改订单管理
     *
     * @param orderPayInfo 订单管理
     * @return 结果
     */
    @Override
    public int updateOrderPayInfo(OrderPayInfo orderPayInfo)
    {
        return orderPayInfoMapper.updateOrderPayInfo(orderPayInfo);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderNums 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderPayInfoByOrderNums(String orderNums)
    {
        return orderPayInfoMapper.deleteOrderPayInfoByOrderNums(Convert.toStrArray(orderNums));
    }

    /**
     * 删除订单管理信息
     *
     * @param orderNum 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderPayInfoByOrderNum(String orderNum)
    {
        return orderPayInfoMapper.deleteOrderPayInfoByOrderNum(orderNum);
    }

    @Override
    public OrderPayInfo selectOneOrderPayInfoByCreateTime(OrderPayInfo orderPayInfo) {
        return orderPayInfoMapper.selectOneOrderPayInfoByCreateTime(orderPayInfo);
    }

}
